﻿using YooAsset;
namespace LitFramework
{
    public class AssetState_ClearCache : IState
    {
        private ClearUnusedCacheFilesOperation clearOp;

        public StateMechine Master { get; set; }

        public void OnEnter(params object[] values)
        {
            GameManager.Instance.InitState = "HotUpdate ClearCache...";
            GameManager.Instance.InitProgress = 0;

            var package = YooAssets.GetPackage(ProjectConfig.Config.packageName);
            clearOp = package.ClearUnusedCacheFilesAsync();
            clearOp.Completed += OnClearComplete;
        }

        public void OnExit()
        {
            clearOp.Completed -= OnClearComplete;
        }

        public void OnUpdate()
        {
            GameManager.Instance.InitProgress = clearOp.Progress;
        }

        private void OnClearComplete(AsyncOperationBase op)
        {
            Master.TurnToState<AssetState_UpdateOver>();
        }
    }
}